<?php 

include_once '/var/www/html/new/d/module/api/ctrip/ctrip.class.php';
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$ctrip = new Ctrip();
$PFT_infunction = new AllFunction();

// $txt=json_encode($_REQUEST);
// $ctrip->logfile($txt,'ctrip_get_data');

//{"VenueID":"","RoundID":"","AreaID":"","SeatID":"","Tnum":"1","Fid":"4274","PayStatus":"1","TicketID":"23385","TouristName":"\u6d4b\u8bd5pzp","TouristTel":"15959110796","PlayDate":"2015-08-26 00:00:00","IdCard":"0"}
// {"VenueID":"","RoundID":"","AreaID":"","SeatID":"","Tnum":"1","Fid":"4274","PayStatus":"1","TicketID":"22229","TouristName":"\u6d4b\u8bd5pzp","TouristTel":"15959110796","PlayDate":"2015-09-15 00:00:00","IdCard":"0"}
// http://www.12301.cc/module/api/ctrip/ctrip_get_data.php?Tnum=1&Fid=4272&PayStatus=1&TicketID=22229&TouristName=pzp&TouristTel=15959110796&PlayDate=2015-09-15 00:00:00

$ctrip->logfile(json_encode($_REQUEST),'ctrip_get_data'); //接收记录

if(!isset($_REQUEST['Action']) && $_REQUEST['PayStatus'] == 1){
	
	$tid = $_REQUEST['TicketID'] + 0;
	$selid = "select uuid,pid,tprice from uu_jq_ticket where id=".$tid." limit 1";
	$GLOBALS['le']->query($selid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('101');		
	$uuids = explode("_",$GLOBALS['le']->f('uuid'));  //查询票类id	
	$ctrip_pid = $uuids[0]; //携程产品id
	$ctrip_rid = $uuids[1]; //携程资源id
	$pid = $GLOBALS['le']->f('pid');  //pid

	$sellPrice_ldata = $PFT_infunction->Dynamic_Price_And_Storage('123697',$pid,substr($_REQUEST['PlayDate'],0,10),0,1,1,"27491");
	if($sellPrice_ldata == 105 || $sellPrice_ldata == 1065) exit('1065|0|0|0|0');
	$sellPrice_ldata_xml =simpleXML_load_string($sellPrice_ldata);
	$sellPrice = (string)$sellPrice_ldata_xml->Rec->UUsprice;
	$gprice = $sellPrice/100;
	
	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$request_array = array(
		'pftordername'	=> $_REQUEST['TouristName'],
		'UUordertel'	=> $_REQUEST['TouristTel'],
		'IDcar'			=> $_REQUEST['IdCard'],
		'pftordernum'	=> $ls_order,
		'UUordertel'	=> $_REQUEST['TouristTel'],
		'ProductID'		=> $ctrip_pid,
		'TicketID'		=> $ctrip_rid,
		'UUplaytime'	=> substr($_REQUEST['PlayDate'],0,10),
		'UUtnum'		=> $_REQUEST['Tnum'],
		'UUlprice'		=> $gprice
	);
	$request = $ctrip->TicketOrderCreateForPrepay_V2($request_array);
	// print_r($request );exit;
	if($request == 101)	exit("101");		
	else{
		$ctrip->logfile($ls_order.'|'.$request['IsSucceed'],'ctrip_submit_success'); //下单成功记录
		exit("200|0|0|0|0|".$ls_order);		
	}	
	 
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功先记录订单数据

	$ordernum = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($ordernum);
	$tordernum = (string)$pft_order_xml->Rec->UUtordernum;
	$tnum = (string)$pft_order_xml->Rec->UUtnum;
	
	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$ordernum','$tordernum','$tordernum',2,0,$tnum,'$tordernum',0,now())"; //携程下单成功没有订单号记录，将临时订单记录
	$GLOBALS['le']->query($str);
}elseif($_REQUEST['Action'] == 'MOD'){
	if($_REQUEST['Tnum'] !=0) exit("1077"); //没有修改订单数量的接口
	$pftOrder = $_REQUEST['Ordern'];
	$selapiOrder = "select apiOrder,handleStatus,oStatus from all_api_order where pftOrder='$pftOrder' limit 1";
	$GLOBALS['le']->query($selapiOrder);
	if(!$GLOBALS['le']->fetch_assoc())	exit('105');
	$apiOrder = $GLOBALS['le']->f('apiOrder');
	$handleStatus = $GLOBALS['le']->f('handleStatus');
	$oStatus = $GLOBALS['le']->f('oStatus');
	if($oStatus == 3) exit("200");
	if($handleStatus == 0 && $oStatus== 0){
		$cancelOrder = $ctrip->TicketUnsubscribeOrder($apiOrder);
		if($cancelOrder) exit("1077");
	}elseif($handleStatus == 1){
		exit("200");
	}
}
else exit("101");
?>exit("200");
	}
}
else exit("101");
?>